﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using RuleCode.Table;

namespace chatblog
{
   
    public partial class bbscata : System.Web.UI.Page
    { 
        cbarea area;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                cbuser.dealcookie();
                dealrequest();
                binduser();
                bind1();
                bind2();
                bind3();
            }
        }
        private void binduser()
        {
            if (AliApi.appUserId == "")
            {
                //Label_lastvisit.Text = "你还未登录,亲登陆.";
                Label_keyongjifen.Visible = false;
                Label_zhuanjiajifen.Visible = false;
                Label1_fatieshu.Visible = false;
                Label1_huitieshu.Visible = false;
            }
            else
            {
                cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
                //Label_lastvisit.Text = "上次登录时间:" + u.jclastdate.Date.ToString();
                Label_keyongjifen.Text = "可用分:" + u.cbpoint.ToString();
                Label_zhuanjiajifen.Text = "专家分:" + u.cbpointzhuanjia.ToString();
                Label1_fatieshu.Text = "发帖数:" + u.fatieshu.ToString();
                Label1_huitieshu.Text = "回帖数:" + u.huitieshu.ToString();
                Label_keyongjifen.Visible = true;
                Label_zhuanjiajifen.Visible = true;
                Label1_fatieshu.Visible = true;
                Label1_huitieshu.Visible = true;
            }
        }
        //是否管理
        private bool canmanage(string cbcataid)
        {
            if (AliApi.appUserId == "")
            {
                return false;
            }
            cbuser u = cbuser.Find(" cbuserid = '" + AliApi.appUserId + "'");
            if (u.isadmin.Value == RuleCode.Enum.NoYes.Yes)
            {
                return true;
            }
            cbcata c = cbcata.Find(" cbcataid = '" + cbcataid + "' ");
            cbbanzhu bz = cbbanzhu.Find(" cbuserid = '" + AliApi.appUserId + "'"
                + " and (cbcataid = '" + c.cbcataid.ToString()
                + "' or cbcataid ='" + c.pid1.ToString()
                + "' or cbcataid ='" + c.pid2.ToString()
                + "' or cbcataid ='" + c.pid3.ToString()
                + "' or cbcataid ='" + c.pid4.ToString()
                + "' )"
                );
            if (bz.Count > 0)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 绑定基础分类
        /// </summary>
        private void bind2()
        {
            cbcata c = cbcata.Find(" cbcataid = '" + this.Request.Params["cbcataid"].ToString() + "' ");
            string sql = "";
            sql += " select 1 as shunxu,* from vcblist where cblisttype = '发帖' ";
            sql += " and zhidingcataid in ( ";
            sql += " select cbcataid from cbcata where cbcataid = '" + this.Request.Params["cbcataid"].ToString()+
                "' or cbcataid = '" + c.pid1.ToString() +
                "' or cbcataid = '" + c.pid2.ToString() +
                "' or cbcataid = '" + c.pid3.ToString() +
                "' or cbcataid = '" + c.pid4.ToString() +
                " ') union "
                ;


            sql += " select top 1000  2 as shunxu, * from vcblist where cblisttype = '发帖' ";
            if (AliApi.appInstanceId != "")
            {
                sql += " and area = '" + AliApi.appInstanceId + "' ";
            }

            if (this.Request.Params["cbcataid"] != null)
            {
                sql += " and cbcataid = '" + this.Request.Params["cbcataid"].ToString() + "' ";
            }
            sql += "  order by shunxu, paixu desc, jccreatedate desc ,jccreatetime desc ";
            DataTable dt = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            WebPager_list.DataSource = dt;
            WebPager_list.DataBind();

            if (canmanage(this.Request.Params["cbcataid"].ToString()))
            {
                this.Panel_manage.Visible = true;
                for (int i = 0; i < this.GridView_list.Rows.Count; i++)
                {
                    CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                    cb.Visible = true;
                }
            }
            else
            {
                this.Panel_manage.Visible = false;
                for (int i = 0; i < this.GridView_list.Rows.Count; i++)
                {
                    CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                    cb.Visible = false ;
                }
            }
            
        }

        private void bind3()
        {
            if (AliApi.appInstanceId == "")
            {
                Label_area.Text = "当前区域:全部";
            }
            else
            {
                Label_area.Text = "当前区域:" + AliApi.appInstanceId;
            }
        }
        private void bind1()
        {
            DataTable a1;
            string sql = "";
            sql = " select * from vcbcata where  " + " pid1 = '" + this.Request.Params["cbcataid"].ToString() + "'";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            //cbcata ct = cbcata.Find(" cbcataid = '" + this.Request.Params["cbcataid"].ToString() + "'");
            GridView_in.DataSource = a1;
            GridView_in.DataBind();

            //今日发帖量
            sql = " select count(*) from vcblist where cbstatus = '有效' "
                + " and jccreatedate <= '" + DateTime.Today.AddDays(1).ToShortDateString()
                + "' and jccreatedate >= '" + DateTime.Today.ToShortDateString() + "' "
               + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' or cbcatastatus = '全部')";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            Label_today.Text = a1.Rows[0].ItemArray[0].ToString();
            //昨日发帖量
            sql = " select count(*) from vcblist where cbstatus = '有效' "
                + " and jccreatedate <= '" + DateTime.Today.ToShortDateString()
                + "' and jccreatedate >= '" + DateTime.Today.AddDays(-1).ToShortDateString() + "' "
                + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' or cbcatastatus = '全部')";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            Label_yestoday.Text = a1.Rows[0].ItemArray[0].ToString();
            //总发帖量
            sql = " select count(*) from vcblist where cbstatus = '有效' "
                + " and ( cbcatastatus = '" + area.cbareaid.ToString()
                + "' or cbcatastatus = '全部')";
            a1 = RuleCode.Common.SqlHelper.Instance.ExecuteQuery(sql).Tables[0];
            Label_sum.Text = a1.Rows[0].ItemArray[0].ToString();
        }
        private void dealrequest()
        {

            area = cbarea.getcurarea();
            HyperLink_link.Text = area.cbtitle.ToString();
            if (this.Request.Params["cbcataid"] == null)
            {
                Response.Redirect(ResolveUrl("~/bbs.html"));
            }
            if (this.Request.Params["cbcataid"] != null)
            {
                Literal_ft.Text =
                "<a href=" + ResolveUrl("~/bbs/bbsedit.aspx?cbcataid=") + this.Request.Params["cbcataid"].ToString()
                    + " onfocus=\"this.blur();\"><img src=\"../images/newtopic.gif\" style=\"border:0;\" /></a> ";
            }
            //HyperLink_ft.NavigateUrl = "bbs/bbsedit.aspx?cbcataid="+this.Request.Params["cbcataid"].ToString();

            string link="";
            cbcata ct = cbcata.Find(" cbcataid = '" + this.Request.Params["cbcataid"].ToString() + "' ");
            this.Title = area.cbtitle.ToString();
            this.Title = ct.cbname.ToString() + "-" + this.Title;
            while (ct.Count >0 &&(ct.pid1.ToString() != "0" 
                && ct.cbcataid.ToString() != area.cbcataid.ToString())||
                ( ct.cbstatus.ToString()=="全部")  )
            {
                link = "-" + "<a href='" + ResolveUrl("~/bbscata/") + ct.cbcataid.ToString() + ".html'>"
                                            +ct.cbname.ToString()+"</a>"+link;
                ct = cbcata.Find(" cbcataid = '" + ct.pid1.ToString() + "' ");
            }
            cbbanzhu bz = cbbanzhu.Find(" cbcataid = '" + this.Request.Params["cbcataid"].ToString() + "' ");
            if (bz.Count < 1)
            {
                link += " 版主:空缺 ";
            }
            else
            {
                link += " 版主: ";
                foreach (DataRow item in bz.MyTable.Rows)
                {
                    cbuser u = cbuser.Find(" cbuserid = '" + item["cbuserid"].ToString()+"'");
                    link += "<a href='" + ResolveUrl("~/blog/") + u.RECID.ToString() + ".html' target=\"_blank\">"
                                + u.cbusernickname.ToString() + "</a> &nbsp;";
                }
            }
            this.Literal_link.Text = link;

            if (area.enableareabtn.Value == RuleCode.Enum.NoYes.No)
            {
                Panel_area.Visible = false;
            }

            Literal_logolink.Text = area.getlogolink();
            cbadv adv = cbadv.Find(" cbuserid = '" + AliApi.appUserId +
                "' and  cbcataid = '" + this.Request.Params["cbcataid"].ToString() + "' ");
            if (adv.Count > 0)
            {
                Literal_adv1.Text = adv.jcdesc1.ToString();
                Literal_adv2.Text = adv.jcdesc2.ToString();
                Literal_adv3.Text = adv.jcdesc3.ToString();
            }
        }

        protected void CheckBox_un_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < this.GridView_list.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                if (cb.Checked == false)
                {
                    cb.Checked = true;
                }
                else
                {
                    cb.Checked = false;
                }
            }
        }

        protected void Button_del_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.GridView_list.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                if (cb.Checked == true)
                {
                    string id = GridView_list.DataKeys[i].Value.ToString();
                    cblist l = cblist.Find(" recid = '" + id + "'");
                    cblist.afterdelete(l);
                    l.Delete();
                }
            }
            bind2();
        }

        protected void WebPager_list_PageIndexChanged(object sender, RuleCode.Controls.PageChangedEventArgs e)
        {
            
            this.WebPager_list.CurrentPageIndex = e.NewPageIndex;
            dealrequest();
            bind2();
        }

        protected void Button_jinghua_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.GridView_list.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                if (cb.Checked == true)
                {
                    string id = GridView_list.DataKeys[i].Value.ToString();
                    cblist l = cblist.Find(" recid = '" + id + "'");
                    l.jinghua.Value = RuleCode.Enum.NoYes.Yes;
                    l.Modify();
                }
            }
            bind2();
            MessageBox.Show(this.Page, "操作完成");
        }

        protected void Button_unjinghua_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.GridView_list.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                if (cb.Checked == true)
                {
                    string id = GridView_list.DataKeys[i].Value.ToString();
                    cblist l = cblist.Find(" recid = '" + id + "'");
                    l.jinghua.Value = RuleCode.Enum.NoYes.No;
                    l.Modify();
                }
            }
            bind2();
            MessageBox.Show(this.Page, "操作完成");
        }

        protected void Button_all_Click(object sender, EventArgs e)
        {
            AliApi.appInstanceId = "";
            bind2();
            bind3();
        }

        protected void Button_bj_Click(object sender, EventArgs e)
        {
            AliApi.appInstanceId = "北京";
            bind2();
            bind3();
        }

        protected void Button_sh_Click(object sender, EventArgs e)
        {
            AliApi.appInstanceId = "上海";
            bind2();
            bind3();
        }

        protected void Button_gz_Click(object sender, EventArgs e)
        {
            AliApi.appInstanceId = "广东";
            bind2();
            bind3();
        }

        protected void Button_qt_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this.Button_qt, this.Button_qt.GetType(), "selectbuttonclick",
                "<script type =\"text/javascript\" >selectButtonClick('" + this.Label_area.ClientID + "');</script>", false);
            bind2();
            bind3();
        }

        protected void Button_zhiding_Click(object sender, EventArgs e)
        {

            string id = "";
            for (int i = 0; i < this.GridView_list.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                if (cb.Checked == true)
                {
                    id += " or (recid =" + GridView_list.DataKeys[i].Value.ToString() + ") ";
                }
            }
            ScriptManager.RegisterStartupScript(this.Button_zhiding, this.Button_zhiding.GetType(), "selectButtonClickzhiding",
                "<script type =\"text/javascript\" >selectButtonClickzhiding('" + id + "');</script>", false);
        }

        protected void Button_unzhiding_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.GridView_list.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                if (cb.Checked == true)
                {
                    string id = GridView_list.DataKeys[i].Value.ToString();
                    cblist l = cblist.Find(" recid = '" + id + "'");
                    if (l.zhidingcataid.ToString() == this.Request.Params["cbcataid"].ToString())
                    {
                        l.zhidingcataid.Value = "";
                        l.Modify();
                    }
                }
            }
            bind2();
            MessageBox.Show(this.Page, "操作完成");
        }

        protected void Button_indefault_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.GridView_list.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                if (cb.Checked == true)
                {
                    string id = GridView_list.DataKeys[i].Value.ToString();
                    cblist l = cblist.Find(" recid = '" + id + "'");
                    l.indefault.Value = RuleCode.Enum.NoYes.Yes;
                    l.Modify();
                }
            }
            bind2();
            MessageBox.Show(this.Page, "操作完成");
        }

        protected void Button_outdefault_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.GridView_list.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                if (cb.Checked == true)
                {
                    string id = GridView_list.DataKeys[i].Value.ToString();
                    cblist l = cblist.Find(" recid = '" + id + "'");
                    l.indefault.Value = RuleCode.Enum.NoYes.No;
                    l.Modify();
                }
            }
            bind2();
            MessageBox.Show(this.Page, "操作完成");
        }

        protected void Button_czhiding_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.GridView_list.Rows.Count; i++)
            {
                CheckBox cb = (CheckBox)this.GridView_list.Rows[i].FindControl("CheckBox_sel");
                if (cb.Checked == true)
                {
                    string id = GridView_list.DataKeys[i].Value.ToString();
                    cblist l = cblist.Find(" recid = '" + id + "'");
                    l.zhidingcataid.Value = this.Request.Params["cbcataid"].ToString();
                    l.Modify();
                }
            }
            bind2();
            MessageBox.Show(this.Page, "操作完成");
        }
    }
}
